package com.xy.blog.user.controller;

import com.xy.blog.core.exception.BaseException;
import com.xy.blog.core.springmvc.controller.BaseController;
import com.xy.blog.core.springmvc.controller.IBaseController;
import com.xy.blog.core.springmvc.result.page.PageReq;
import com.xy.blog.user.controller.model.req.OrganizationReq;
import com.xy.blog.user.controller.model.req.query.OrganizationQuery;
import com.xy.blog.user.mapstruct.OrganizationConverter;
import com.xy.blog.user.service.impl.OrganizationServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @author zanhonglei
 * @version V1.0
 * @Description:
 * @date 2020/3/7
 */
@Api(value = "OrganizationController", tags = "机构管理")
@RequestMapping("/v1/organization")
@RestController
public class OrganizationController extends BaseController implements IBaseController<OrganizationQuery, OrganizationReq> {

    @Autowired
    private OrganizationServiceImpl organizationServiceImpl;

    @Autowired
    private OrganizationConverter organizationConverter;

    @Override
    public Object pages( PageReq<OrganizationQuery> req) throws BaseException {
        return getPageData(organizationConverter.bo2Vo(organizationServiceImpl.listByPage(req)));
    }



    @Override
    public Object getById(Long id) throws BaseException {
        return getData(organizationConverter.bo2Vo(organizationServiceImpl.getById(id)));
    }

    @Override
    public Object save(@Valid OrganizationReq req) throws BaseException {
        organizationServiceImpl.save(organizationConverter.req2Bo(req));
        return success();
    }

    @Override
    public Object update(@Valid OrganizationReq req) throws BaseException {
        organizationServiceImpl.update(organizationConverter.req2Bo(req));
        return success();
    }

    @Override
    public Object deleteById(Long id) throws BaseException {
        organizationServiceImpl.deleteById(id);
        return success();
    }

    @GetMapping("/list-all")
    @ApiOperation(value = "查询所有", notes = "查询所有", httpMethod = "GET")
    public Object listByName(String name) {
        return getData(organizationConverter.bo2Vo(organizationServiceImpl.listByName(name)));
    }
}
